﻿using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;
using MM.Common.Base;
using MM.Common.DBBase;
using MM.Common.Setup;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MM.Common.Support
{
    public partial class FrmParameterSetup : FrmSetupCommon
    {
        public FrmParameterSetup()
        {
            InitializeComponent();
        }
        private void DisplayDetail(string pname)
        {
            DataTable dt = new DataTable();
            SystemInfo.DbData.Retrieve("SELECT PARAMETER_NAME2 ,DESCRIBE FROM MXHLY_SYS_PARAMETER_RELATION WHERE PARAMETER_NAME1 = :pname", new DataParamCol(new DataParam[] { new InputDataParam("pname", pname) }), ref dt);
            this.lstFrom.DataSource = dt;
            this.lstFrom.DisplayMember = "PARAMETER_NAME2";
            DataTable table2 = new DataTable();
            SystemInfo.DbData.Retrieve("SELECT PARAMETER_NAME1 ,DESCRIBE FROM MXHLY_SYS_PARAMETER_RELATION WHERE PARAMETER_NAME2 = :pname", new DataParamCol(new DataParam[] { new InputDataParam("pname", pname) }), ref table2);
            this.lstTo.DataSource = table2;
            this.lstTo.DisplayMember = "PARAMETER_NAME1";
            DataTable table3 = new DataTable();
            SystemInfo.DbData.Retrieve("SELECT FORM_CODE || ISNULL((SELECT FORM_NAME FROM KTSYS_FORM WHERE FORM_CODE = P.FORM_CODE) ,'') F_NAME FROM KTSYS_FORM_PARAMETER P WHERE PARAMETER_NAME = :pname", new DataParamCol(new DataParam[] { new InputDataParam("pname", pname) }), ref table3);
            this.lstWindow.DataSource = table3;
            this.lstWindow.DisplayMember = "F_NAME";
        }
        private void FrmParameterSetup_Load(object sender, EventArgs e)
        {
            this.gvParameter.Columns["PARAMETER_NAME_CHN"].Tag = "ON";
            this.gvParameter.Columns["DESCRIBE"].Tag = "ON";
            base.gvSetup = this.gvParameter;
            string selsql = "SELECT PARAMETER_NAME, PARAMETER_NAME_CHN, PARAMETER_VALUE, PARAMETER_TYPE, MODIFY_TYPE ,RESTRICT_TYPE ,DESCRIBE FROM MXHLY_SYS_PARAMETER ORDER BY PARAMETER_NAME ASC ";
            base.setSql(selsql, "MXHLY_SYS_PARAMETER", new string[] { "PARAMETER_NAME" }, null, "", "");
            DataTable table = new DataTable();
            table.Columns.Add("PCODE", typeof(string));
            table.Columns.Add("PNAME", typeof(string));
            table.Rows.Add(new object[] { "C", "字符" });
            table.Rows.Add(new object[] { "N", "数字" });
            table.Rows.Add(new object[] { "D", "日期" });
            this.riParameterType.DataSource = table;
            this.riParameterType.DisplayMember = "PNAME";
            this.riParameterType.ValueMember = "PCODE";
            DataTable table2 = new DataTable();
            table2.Columns.Add("MCODE", typeof(string));
            table2.Columns.Add("MNAME", typeof(string));
            table2.Rows.Add(new object[] { "Y", "可修改" });
            table2.Rows.Add(new object[] { "N", "不能修改" });
            table2.Rows.Add(new object[] { "W", "警告" });
            this.riModifyType.DataSource = table2;
            this.riModifyType.DisplayMember = "MNAME";
            this.riModifyType.ValueMember = "MCODE";
            DataTable table3 = new DataTable();
            table3.Columns.Add("RCODE", typeof(string));
            table3.Columns.Add("RNAME", typeof(string));
            table3.Rows.Add(new object[] { "SYSTEM", "系统" });
            table3.Rows.Add(new object[] { "COMPUTER", "机器" });
            table3.Rows.Add(new object[] { "PERSONAL", "个人" });
            this.riRestrictType.DataSource = table3;
            this.riRestrictType.DisplayMember = "RNAME";
            this.riRestrictType.ValueMember = "RCODE";
            base.ZoomWin();
            this.freshData();
        }

        private void gvSetup_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            if ((e.Column.Name == "gcParameterValue") && (this.gvParameter.GetFocusedDataRow()["MODIFY_TYPE"].ToString() == "W"))
            {
                XtraMessageBox.Show("数据已经改变!", "警告");
            }
        }

        private void gvSetup_FocusedRowChanged(object sender, FocusedRowChangedEventArgs e)
        {
            DataRow focusedDataRow = this.gvParameter.GetFocusedDataRow();
            if (focusedDataRow != null)
            {
                string str = focusedDataRow["MODIFY_TYPE"].ToString();
                this.gcParameterName.OptionsColumn.AllowEdit = str == "Y";
                this.gcParameterNameChn.OptionsColumn.AllowEdit = str == "Y";
                this.gcParameterType.OptionsColumn.AllowEdit = str == "Y";
                this.gcRestrictType.OptionsColumn.AllowEdit = str != "N";
                this.gcParameterValue.OptionsColumn.AllowEdit = str != "N";
                this.gcDescribe.OptionsColumn.AllowEdit = str != "N";
                this.DisplayDetail(focusedDataRow["PARAMETER_NAME"].ToString());
            }
        }

        private void lstFrom_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.lstFrom.SelectedIndex;
            if (selectedIndex >= 0)
            {
                this.meFrom.Text = ((DataTable)this.lstFrom.DataSource).Rows[selectedIndex]["DESCRIBE"].ToString();
            }
        }

        private void lstTo_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.lstTo.SelectedIndex;
            if (selectedIndex >= 0)
            {
                this.meTo.Text = ((DataTable)this.lstTo.DataSource).Rows[selectedIndex]["DESCRIBE"].ToString();
            }
        }
    }
}
